﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using CIRDiagnoseDevice.DBManage;
using DevExpress.XtraGrid.Views.Grid;
using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;

namespace CIRDiagnoseDevice.Test
{
    class _450MFunTrial
    {
        #   region 全局变量

        private TestRecordManage manage = new TestRecordManage();
        private int Cycle;
        SerialPort OSP4;
        Form form;
        public int ReadedNum = 0; // 记录接收返回数据的次数
        public int CycleTimes = 0;
        public event MessageNotice OnMessageNotice;
        private byte[] RBuffer = new byte[500]; // 接收测试仪返回结果
        private DataGridView dataGridView8;
        private List<byte> ListBuffer = new List<byte>();//缓存接收数据
        private LBJAndTrainEndTest Lbj = new LBJAndTrainEndTest();
        private CommunicationWithInterfaceUnit unit = new CommunicationWithInterfaceUnit();
        private bool Addressed = true;
        private InitSerialPort InitPort = new InitSerialPort();
        private int initIndex; //填写测试结果时的初始位置
        private Thread priorityThread;
        private string ToolStripInfo;
        private bool haveReceiveCabNum;
        byte cabNum1 = 0, cabNum2 = 0, cabNum3 = 0, cabNum4 = 0;
        private CIR_450M_TranTest C_450M = new CIR_450M_TranTest();
        private System.Timers.Timer timer = new System.Timers.Timer();
        private bool haveWaitValidateConn, ValidateConnSucc, haveQueryWndPressure, haveAirExhaust, haveReceiveWndAlarm, haveReceiveVolAlarm, haveDeConnection;
        private static bool timerEnable = true;
        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //用于找到MessageBox的句柄
        [DllImport("user32.dll")]
        static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);
        #   endregion

        public _450MFunTrial(Form form, DataGridView dataGridView8, int initIndex, SerialPort OSP, Thread priorityThread)
        {
            this.form = form;
            this.dataGridView8 = dataGridView8;
            this.OSP4 = OSP;
            this.initIndex = initIndex;
            this.priorityThread = priorityThread;
        }

        public void ClearSource()
        {
            OSP4.Close();
            OSP4.DataReceived -= new SerialDataReceivedEventHandler(OSP4_DataReceived);
            GC.Collect();
        }

        public static bool TimerEnable
        {
            set { timerEnable = value; }
        }

        public void InitParameter()
        {
            Cycle = 0;
            CycleTimes = 0;
            ReadedNum = 0;
            Addressed = true;
            haveWaitValidateConn = false;
            ValidateConnSucc = false;
            haveQueryWndPressure = false;
            haveAirExhaust = false;
            haveReceiveVolAlarm = false;
            haveReceiveWndAlarm = false;
            haveReceiveCabNum = false;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 25000;
            Array.Clear(RBuffer, 0, RBuffer.Length);
            ListBuffer.Clear();
            MainFrm.MotherFrm.TestTrainEnd = false;
            this.OSP4.DataReceived += new SerialDataReceivedEventHandler(OSP4_DataReceived);
            this.OSP4.ReadTimeout = 60000;
            InitPort.Open(OSP4);
        }

        # region 关闭MessageBox窗体
        private void CloseMessageBox(string formText)
        {
            IntPtr ptr = FindWindow(null, formText);//查找MessageBox的弹出窗口,注意对应标题
            if (ptr != IntPtr.Zero)
            {
                IntPtr result;
                EndDialog(ptr, out result); //查找到窗口则关闭
            }

        }

        #endregion


        private void Start450MFunTrial()
        {
            if (MessageBox.Show("现在进行450M列尾功能试验，请选择频率为458.2MHZ运行线路，\r\n\r\n并将CIR连接到450M列尾试验单元且将MMI扬声器改为内置！", "用户操作提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                Delay(1000);
                SendConnectRequest();   // 发送450M列尾连接命令
                timer.Enabled = true;
                MessageBox.Show("请听到列尾装置等待确认语音提示或者看到MMI上文字提示信息后先点击“确定”再按MMI上的“列尾确认”按键", "用户操作提示");
                haveWaitValidateConn = true;
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (MainFrm.MotherFrm.TestTrainEnd)
            {
                if (timerEnable == false)
                {
                    timer.Enabled = false;
                  //  return;
                }
                if (ReadedNum == 0)
                {
                    if (!haveWaitValidateConn)
                    {
                        if (Cycle != 2)
                        {
                            SendConnectRequest();
                            Cycle++;
                            return;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                        }
                    }
                    else if (!ValidateConnSucc)
                    {
                        if (Cycle != 2)
                        {
                            C_450M.SendConnSuccessRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                            Cycle++;
                            return;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                            CloseMessageBox("用户操作提示");
                            MessageBox.Show("列尾连接失败无法进行后面功能试验，\r\n\r\n请检查串口和列尾试验单元是否连接正确后再重新测试", "提示");
                            for (int i = 0; i < 6; i++)
                            {
                                FillGridWithJudgeResult(dataGridView8, i + initIndex, "不合格", false);
                            }
                            if (MainFrm.MotherFrm.IsTestTotal)
                            {
                                MainFrm.MotherFrm.TestTotalSaved = false;
                                MainFrm.MotherFrm.WholeFunTrialSaved = false;
                            }
                            ReadedNum = 0;
                            if (!MainFrm.MotherFrm.IsTestTotal)
                            {
                                MainFrm.MotherFrm.ChangeButtonText();
                            }
                            MainFrm.MotherFrm.TrainEndTrialSaved = true;
                            MainFrm.MotherFrm.TestTrainEnd = false;
                            MainFrm.MotherFrm.StopTest = true;
                            MainFrm.MotherFrm.Suspend = true;
                            timerEnable = false;
                            OSP4.Close();
                            OSP4.DataReceived -= new SerialDataReceivedEventHandler(OSP4_DataReceived);
                            MessageBox.Show("列尾功能测试完成!");
                            if (OnMessageNotice != null)
                            {
                                OnMessageNotice(this, ThreadRunState.End);
                            }
                            return;
                        }
                    }
                }
                else if (ReadedNum == 1)
                {
                    if (!haveQueryWndPressure)
                    {
                        if (Cycle != 2)
                        {
                            C_450M.SendWndQueryRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4, 999);
                            Cycle++;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                        }
                    }
                }
                else if (ReadedNum == 2)
                {
                    if (!haveAirExhaust)
                    {
                        if (Cycle != 2)
                        {
                            C_450M.SendAirExhaustRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                            Cycle++;
                            return;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                        }
                    }
                    else if (!haveReceiveWndAlarm)
                    {
                        if (Cycle != 2)
                        {
                            SendWndPressureAlarm();
                            Cycle++;
                            return;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                            haveReceiveWndAlarm = true;
                            FillGridWithJudgeResult(dataGridView8, 3 + initIndex, "不合格", false);
                            Delay(4000);
                            SendVoltageAlarm();
                            MessageBox.Show("请听到CIR报电量不足后先点击“确定”再按 “列尾确认”", "提示");
                            timer.Enabled = true;
                            ReadedNum++;
                        }
                    }
                }
                else if (ReadedNum == 3)
                {
                    if (!haveReceiveVolAlarm)
                    {
                        if (Cycle != 2)
                        {
                            SendVoltageAlarm();
                            Cycle++;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                            haveReceiveVolAlarm = true;
                            FillGridWithJudgeResult(dataGridView8, 4 + initIndex, "不合格", false);
                            MessageBox.Show("请先点击“确定”再长按MMI上的“列尾消号”按键", "提示");
                            ReadedNum++;
                        }
                    }
                }
                else if (ReadedNum == 5)
                {
                    if (!haveDeConnection)
                    {
                        if (Cycle != 2)
                        {
                            C_450M.SendDeConnection(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                            Cycle++;
                        }
                        else
                        {
                            Cycle = 0;
                            timer.Enabled = false;
                        }
                    }
                }
            }
        }

        //发送列尾连接请求
        public void SendConnectRequest()
        {
            MainFrm.MotherFrm.TestTrainEnd = true;
            C_450M.SendConnReq(OSP4);
        }

        // 发送风压报警
        public void SendWndPressureAlarm()
        {
            C_450M.SendWndPressureAlarm(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
            string statuStr = "正在试验450M列尾风压报警功能";
            MainFrm.MotherFrm.SetToolStrip(statuStr);
        }

        //发送电压报警
        public void SendVoltageAlarm()
        {
            C_450M.SendVoltageAlarm(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
        }


        #region    处理450M列尾返回数据

        void OSP4_DataReceived(object sender, SerialDataReceivedEventArgs e)    //COM2口
        {
            while (!MainFrm.MotherFrm.StopTest)
            {
                while (!MainFrm.MotherFrm.Suspend)
                {
                    int readNum = 0;
                   
                    if (MainFrm.MotherFrm.TestTrainEnd)
                    {
                        try
                        {
                            readNum = OSP4.Read(RBuffer, 0, 200);
                        }
                        catch (Exception e1)
                        {
                            if (MainFrm.MotherFrm.TestTrainEnd)
                            {
                                if (e1.GetType() == typeof(TimeoutException) && MainFrm.MotherFrm.TestTrainEnd)
                                {
                                    CloseMessageBox("提示");
                                    if (ReadedNum == 0)
                                    {
                                        if (!MainFrm.MotherFrm.IsTestTotal)
                                        {
                                            MainFrm.MotherFrm.ChangeButtonText();
                                        }
                                        MainFrm.MotherFrm.TrainEndTrialSaved = true;
                                        MainFrm.MotherFrm.TestTrainEnd = false;
                                        if (OnMessageNotice != null)
                                        {
                                            OnMessageNotice(this, ThreadRunState.End);
                                        }
                                        MainFrm.MotherFrm.StopTest = true;
                                        MainFrm.MotherFrm.Suspend = true;
                                        timer.Enabled = false;
                                        OSP4.Close();
                                        OSP4.DataReceived -= new SerialDataReceivedEventHandler(OSP4_DataReceived);
                                        MessageBox.Show("450M列尾试验单元与CIR无法通信，请检查是否将之与CIR相连后再重新测试！");

                                    }
                                    else if (ReadedNum == 1)
                                    {

                                        FillGridWithJudgeResult(dataGridView8, 1 + initIndex, "不合格", false);
                                        Cycle = 0;
                                        MessageBox.Show("请先点击“确定”再长按MMI上的“列尾排风”按键", "提示");
                                        ReadedNum++;

                                    }
                                    else if (ReadedNum == 2)
                                    {

                                        FillGridWithJudgeResult(dataGridView8, 2 + initIndex, "不合格", false);
                                        Cycle = 0;
                                        Delay(4000);
                                        SendWndPressureAlarm();
                                        MessageBox.Show("请听到CIR报风压值后点击“确定”再按 “列尾确认”", "提示");
                                        ReadedNum++;

                                    }
                                    else if (ReadedNum == 3)
                                    {
                                        haveReceiveWndAlarm = true;
                                        FillGridWithJudgeResult(dataGridView8, 3 + initIndex, "不合格", false);
                                        Cycle = 0;
                                        Delay(4000);
                                        SendVoltageAlarm();
                                        MessageBox.Show("请听到CIR报电量不足后先点击“确定”再按 “列尾确认”", "提示");
                                        ReadedNum++;

                                    }
                                    else if (ReadedNum == 4)
                                    {
                                        haveReceiveVolAlarm = true;
                                        FillGridWithJudgeResult(dataGridView8, 4 + initIndex, "不合格", false);
                                        Cycle = 0;
                                        MessageBox.Show("请先点击“确定”再长按MMI上的“列尾消号”按键", "提示");
                                        ReadedNum++;

                                    }
                                    else if (ReadedNum == 5)
                                    {

                                        FillGridWithJudgeResult(dataGridView8, 5 + initIndex, "不合格", false);
                                        C_450M.SendDeConnection(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                                        if (MainFrm.MotherFrm.IsTestTotal)
                                        {
                                            MainFrm.MotherFrm.WholeFunTrialSaved = false;
                                        }
                                        ReadedNum = 0;
                                        if (!MainFrm.MotherFrm.IsTestTotal)
                                        {
                                            MainFrm.MotherFrm.TestTotalSaved = false;
                                            MainFrm.MotherFrm.ChangeButtonText();
                                        }
                                        ToolStripInfo = "450M列尾功能试验完成";
                                        MainFrm.MotherFrm.SetToolStrip(ToolStripInfo);
                                        MainFrm.MotherFrm.TrainEndTrialSaved = false;
                                        MainFrm.MotherFrm.TestTrainEnd = false;
                                        MainFrm.MotherFrm.StopTest = true;
                                        MainFrm.MotherFrm.Suspend = true;
                                        timer.Enabled = false;
                                        timerEnable = false;
                                        OSP4.Close();
                                        OSP4.DataReceived -= new SerialDataReceivedEventHandler(OSP4_DataReceived);
                                        MessageBox.Show("列尾功能测试完成!");
                                        if (OnMessageNotice != null)
                                        {
                                            OnMessageNotice(this, ThreadRunState.End);
                                        }
                                        return;

                                    }
                                }
                            }
                        }
                        byte[] temp = new byte[readNum];
                        Array.Copy(RBuffer, 0, temp, 0, readNum);
                        ListBuffer.AddRange(temp);
                    }
                }
            }
        }

        //查找帧头和帧尾
        private int FindDataFrameHead(List<byte> arr, byte head1, byte head2, int startIndex, int findLength, bool isFindHead)
        {
            int index = -1;
            int length = 0;
            length = findLength > arr.Count ? arr.Count : findLength;
            for (int i = startIndex; i < length - 1; i++)
            {
                if (arr[i] == head1 && arr[i + 1] == head2)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }

        public void DealWithReceiveData()
        {
            try
            {
                if (priorityThread != null)
                    priorityThread.Join();
            }
            catch
            {

            }
            while (!MainFrm.MotherFrm.StopTest)
            {
                while (!MainFrm.MotherFrm.Suspend)
                {
                    if (MainFrm.MotherFrm.TestTrainEnd == false)
                    {
                        if (!haveWaitValidateConn)
                        {
                            Start450MFunTrial();
                            ToolStripInfo = "正在试验450M列尾功能";
                            MainFrm.MotherFrm.SetToolStrip(ToolStripInfo);
                        }
                    }
                    if (MainFrm.MotherFrm.TestTrainEnd)
                    {
                        try
                        {
                            if (ListBuffer.Count > 12)
                            {
                                int startIndex = 0;
                                startIndex = FindDataFrameHead(ListBuffer, (byte)0x10, (byte)0x02, 0, ListBuffer.Count, true);
                                if (startIndex < 0)
                                    continue;
                                int endIndex = FindDataFrameHead(ListBuffer, (byte)0x10, (byte)0x03, startIndex, ListBuffer.Count, false);
                                if (endIndex < 0)
                                    continue;
                                int length = endIndex - startIndex + 2;
                                byte[] readData = new byte[length];
                                ListBuffer.CopyTo(startIndex, readData, 0, length);
                                ListBuffer.RemoveRange(0, endIndex + 2);
                                byte[] realData = Lbj.DePacketData(readData);
                                if (realData.Length < 8)
                                    continue;
                                if (realData[7] != 0x07)
                                {
                                    if (realData.Length >= 20)
                                    {
                                        if (haveReceiveCabNum == false)
                                        {
                                            haveReceiveCabNum = true;
                                            MainFrm.MotherFrm.CabNum1 = cabNum1 = realData[8];
                                            MainFrm.MotherFrm.CabNum2 = cabNum2 = (byte)(realData[9]);
                                            MainFrm.MotherFrm.CabNum3 = cabNum3 = realData[10];
                                            MainFrm.MotherFrm.CabNum4 = cabNum4 = realData[11];
                                        }
                                    }
                                }
                                if (realData[7] == 0x01)  //ReadNum =0
                                {
                                    //回连接成功
                                    if (Addressed)
                                    {
                                        Delay(800);
                                        timer.Enabled = false;
                                        C_450M.SendConnSuccessRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                                        timer.Enabled = true;
                                        FillGridWithJudgeResult(dataGridView8, 0 + initIndex, "合格", true);
                                        MessageBox.Show("请听到CIR报列尾确认完毕后点击“确定”再按MMI上的“风压查询”按键", "提示");
                                        ValidateConnSucc = true;
                                        timer.Enabled = false;
                                        ReadedNum++;
                                        Addressed = false;
                                        Cycle = 0;
                                    }
                                }
                                else if (realData[7] == 0x02) //ReadNum =1
                                {
                                    if (Addressed == false)
                                    {
                                        timer.Enabled = true;
                                        C_450M.SendWndQueryRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4, 999);
                                        FillGridWithJudgeResult(dataGridView8, 1 + initIndex, "合格", true);
                                        MessageBox.Show("请听到CIR报风压值后点击“确定”再长按MMI上的“列尾排风”按键", "提示");
                                        ReadedNum++;
                                        Cycle = 0;
                                        timer.Enabled = false;
                                        haveQueryWndPressure = true;
                                        Addressed = true;
                                    }
                                }
                                else if (realData[7] == 0x03) // ReadNum = 2
                                {
                                    if (Addressed == true)
                                    {
                                        timer.Enabled = true;
                                        C_450M.SendAirExhaustRes(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                                        MessageBox.Show("请听到CIR报列尾排风后点击“确定”再按MMI上的“列尾确认”", "提示");
                                        haveAirExhaust = true;
                                        timer.Enabled = false;
                                        Cycle = 0;
                                        FillGridWithJudgeResult(dataGridView8, 2 + initIndex, "合格", true);
                                        Delay(4000);
                                        timer.Enabled = true;
                                        SendWndPressureAlarm();
                                        MessageBox.Show("请听到CIR报风压值后先点击“确定”再按MMI上的 “列尾确认”", "提示");
                                        if (MainFrm.MotherFrm.ManufactureName == "天津七一二" || MainFrm.MotherFrm.ManufactureName == "上海通信")
                                        {
                                            FillGridWithJudgeResult(dataGridView8, 3 + initIndex, "合格", true);
                                            Delay(6000);
                                            timer.Enabled = true;
                                            SendVoltageAlarm();
                                            MessageBox.Show("请听到CIR报电量不足后先点击“确定”再按MMI上的 “列尾确认”", "提示");
                                            if (MainFrm.MotherFrm.ManufactureName == "天津七一二" || MainFrm.MotherFrm.ManufactureName == "上海通信")
                                            {
                                                if (Addressed == true)
                                                {
                                                    FillGridWithJudgeResult(dataGridView8, 4 + initIndex, "合格", true);
                                                    MessageBox.Show("请先点击“确定”再长按MMI上的“列尾消号”按键", "提示");
                                                    Addressed = false;
                                                    ReadedNum++;
                                                }
                                            }
                                        }
                                        ReadedNum++;
                                        haveReceiveWndAlarm = true;
                                        timer.Enabled = false;
                                        Cycle = 0;
                                        Addressed = false;
                                    }
                                }
                                else if (realData[7] == 0x04) //ReadNum = 3
                                {
                                    if (Addressed == false)
                                    {
                                        FillGridWithJudgeResult(dataGridView8, 3 + initIndex, "合格", true);
                                        Delay(4000);
                                        timer.Enabled = true;
                                        SendVoltageAlarm();
                                        MessageBox.Show("请听到CIR报电量不足后先点击“确定”再按MMI上的 “列尾确认”", "提示");
                                        ReadedNum++;
                                        timer.Enabled = false;
                                        Cycle = 0;
                                        haveReceiveVolAlarm = true;
                                        Addressed = true;
                                    }
                                }
                                else if (realData[7] == 0x05) //ReadNum =4
                                {
                                    if (Addressed == true)
                                    {
                                        FillGridWithJudgeResult(dataGridView8, 4 + initIndex, "合格", true);
                                        MessageBox.Show("请先点击“确定”再长按MMI上的“列尾消号”按键", "提示");
                                        ReadedNum++;
                                        Addressed = false;
                                    }
                                }
                                else if (realData[7] == 0x06) //ReadNum =5
                                {
                                    if (Addressed == false)
                                    {
                                        C_450M.SendDeConnection(OSP4, cabNum1, cabNum2, cabNum3, cabNum4);
                                        timer.Enabled = true;
                                        MessageBox.Show("请听到CIR报列尾消号成功后点击“确定”", "提示");
                                        haveDeConnection = true;
                                        timer.Enabled = false;
                                        FillGridWithJudgeResult(dataGridView8, 5 + initIndex, "合格", true);
                                        if (MainFrm.MotherFrm.IsTestTotal)
                                        {
                                            MainFrm.MotherFrm.TestTotalSaved = false;
                                            MainFrm.MotherFrm.WholeFunTrialSaved = false;
                                        }
                                        ReadedNum = 0;
                                        if (!MainFrm.MotherFrm.IsTestTotal)
                                        {
                                            MainFrm.MotherFrm.ChangeButtonText();
                                        }
                                        ToolStripInfo = "450M列尾功能试验完成";
                                        MainFrm.MotherFrm.SetToolStrip(ToolStripInfo);
                                        MainFrm.MotherFrm.TrainEndTrialSaved = false;
                                        MainFrm.MotherFrm.TestTrainEnd = false;
                                        MainFrm.MotherFrm.StopTest = true;
                                        MainFrm.MotherFrm.Suspend = true;
                                        timerEnable = false;
                                        OSP4.Close();
                                        OSP4.DataReceived -= new SerialDataReceivedEventHandler(OSP4_DataReceived);
                                        MessageBox.Show("列尾功能测试完成!");
                                        if (OnMessageNotice != null)
                                        {
                                            OnMessageNotice(this, ThreadRunState.End);
                                        }
                                        return;
                                    }
                                }
                                else if (realData[7] == 0x07)
                                {
                                }
                            }
                        }
                        catch (Exception e1)
                        {

                        }
                    }
                }
            }
        }
        #endregion


        //将测试判断结果填写到测试表中
        private void FillGridWithJudgeResult(DataGridView gridview, int rowIndex, string result, bool isGreen)
        {
            if (gridview.InvokeRequired)
            {
                gridview.Invoke(new MethodInvoker(delegate
                {
                    gridview.Rows[rowIndex].Cells["试验结果"].Value = result;
                    if (isGreen)
                        gridview.Rows[rowIndex].Cells["试验结果"].Style.ForeColor = Color.Green;
                    else
                        gridview.Rows[rowIndex].Cells["试验结果"].Style.ForeColor = Color.Red;
                    gridview.Refresh();
                }));
            }
            else
            {
                gridview.Rows[rowIndex].Cells["试验结果"].Value = result;
                if (isGreen)
                    gridview.Rows[rowIndex].Cells["试验结果"].Style.ForeColor = Color.Green;
                else
                    gridview.Rows[rowIndex].Cells["试验结果"].Style.ForeColor = Color.Red;
                gridview.Refresh();
            }
        }

        //延时多少毫秒
        public static void Delay(int mSecond)
        {
            int last_start;

            last_start = Environment.TickCount;
            while ((Environment.TickCount - last_start) <= mSecond)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        }
    }
}
